package com.zoke.support.image.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.GridView;

import com.zoke.support.R;
import com.zoke.support.base.BaseActivity;
import com.zoke.support.config.ZokeConfig;
import com.zoke.support.image.ShowImageBeen;
import com.zoke.support.image.ShowImagesAdapter;
import com.zoke.support.image.ShowImagesAdapter.AdaterListener;
import com.zoke.support.image.activity.GalleryActivity.HandleType;
import com.zoke.support.image.activity.GalleryActivity.PhotoRequestCode;

/**
 * 查看相册文件夹
 * 
 * @author JackWu
 * @version
 * @date 2014-11-19
 * @_QQ_ 651319154
 */
public class ShowImagesActivity extends BaseActivity implements AdaterListener {
	public static final String PHOTOSFIRSNAME = "_filename";// 标题名
	public static final String PHOTOSDATASSTRINGS = "_datalists";// 文件夹内的图片集
	public static final String MAXMORE = "_maxmore";//最多还能支持多少照片 不传默认是9张
	private int mMax;
	private GridView mGridView;
	private ShowImagesAdapter mAdapter;
	private List<ShowImageBeen> mList = new ArrayList<ShowImageBeen>();

	private int mType;// 操作类型

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_showimages);
		String title = getIntent().getStringExtra(PHOTOSFIRSNAME);
		mType = getIntent().getIntExtra(GalleryActivity.HANDLETYPE, 1);// 默认是不裁剪的单张图
		mMax = getIntent().getIntExtra(MAXMORE, 9);//默認支持9張
		mEnableSwipeRight = true;
		mTitleManager.setLeftIcon(R.drawable.app_title_back).setTitle(title);
		if (mType == HandleType.MORE) {
			mTitleManager.setRightText("完成");
		}
		mGridView = (GridView) findViewById(R.id.child_grid);
		mAdapter = new ShowImagesAdapter(ShowImagesActivity.this, mList, mType,
				this,mMax);
		mGridView.setAdapter(mAdapter);
		List<String> list = getIntent().getStringArrayListExtra(
				PHOTOSDATASSTRINGS);
		exchangeData(list);
		mAdapter.notifyDataSetChanged();
	}

	@Override
	protected void handleMsg(Message msg) {
		super.handleMsg(msg);
		int id = msg.what;
		if (id == ZokeConfig.UPDATEIMAGECOUNT) {
			// 更新title
			int count = (Integer) msg.obj;
			if (count == 0) {
				mTitleManager.setRightText("完成");
				return;
			}
			mTitleManager.setRightText("完成(" + count + ")");
			return;
		}
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(data==null){
			return;
		}
		setResult(resultCode, data);
		ShowImagesActivity.this.onBackPressed();
	}

	@Override
	public void onTitleClick(View view) {
		super.onTitleClick(view);
		if (mGridView != null && mList.size() != 0)
			mGridView.smoothScrollToPosition(0);
	}

	@Override
	public void onTitleClickRight(View view) {
		super.onTitleClickRight(view);
		// 只有more时按钮才会显示
		Intent data = new Intent();
		data.putStringArrayListExtra(PhotoRequestCode.REQUESTKEY,
				getCheckedString());
		setResult(PhotoRequestCode.MORE, data);
		ShowImagesActivity.this.onBackPressed();
	}

	/** 获取选中的图 **/
	private ArrayList<String> getCheckedString() {
		ArrayList<String> list = new ArrayList<String>();
		for (ShowImageBeen been : mList) {
			if (been.isChecked()) {
				list.add(been.getPath());
			}
		}
		return list;
	}

	/** 转换数据 **/
	private void exchangeData(List<String> list) {
		if (list != null && list.size() != 0) {
			for (String str : list) {
				ShowImageBeen been = new ShowImageBeen();
				been.setChecked(false);
				been.setPath(str);
				mList.add(been);
			}
		}
	}

	@Override
	public void onAdapterItemClick(int position, Object obj) {
		ShowImageBeen been = (ShowImageBeen) obj;
		if (mType == HandleType.CROP) {
			// 裁剪图片的业务逻辑
			mIntent.setClass(ShowImagesActivity.this, ImageCutActivity.class);
			mIntent.putExtra(ImageCutActivity.CUTIMAGEPATH, been.getPath());
			startUIForResult(mIntent, PhotoRequestCode.CROP);
			return;
		}

		if (mType == HandleType.ONE) {
			// 一张图片的业务逻辑
			Intent data = new Intent();
			data.putExtra(PhotoRequestCode.REQUESTKEY, been.getPath());
			setResult(PhotoRequestCode.ONE, data);
			ShowImagesActivity.this.onBackPressed();
			return;
		}

		if (mType == HandleType.MORE) {
			// 没有操作
			return;
		}

		if (mType == HandleType.FILTER) {
			// 单张并加效果
			return;
		}
	}

}
